package com.xiaotao.controller;


import com.alibaba.fastjson.JSONObject;
import com.nimbusds.jose.JOSEException;
import com.xiaotao.entity.CardVerify;
import com.xiaotao.entity.User;
import com.xiaotao.mapper.UserMapper;
import com.xiaotao.service.UserService;
import com.xiaotao.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    @GetMapping
    public ResponseUtil queryAll() {
        return userService.queryAll();
    }

    @PostMapping
    public ResponseUtil add(@RequestBody User user) {
        return userService.add(user);
    }

    @PutMapping
    public ResponseUtil update(@RequestBody User user) {
        return userService.update(user);
    }

    @DeleteMapping("/{id}")
    public ResponseUtil delete(@PathVariable Integer id) {
        return userService.delete(id);
    }

    @GetMapping("/{id}")
    public ResponseUtil queryById(@PathVariable Integer id) {
        return userService.queryById(id);
    }

    @PostMapping("/registered")
    public ResponseUtil registered(@RequestBody User user) {
        return userService.registered(user);
    }

    @PostMapping("/login")
    public ResponseUtil login(@RequestBody User user) throws JOSEException {
        return userService.login(user);
    }


    @PostMapping("/verify")
    public ResponseUtil verify(@RequestBody User user) throws Exception {
        return userService.verify(user);
    }

    @GetMapping("/userId")
    public ResponseUtil queryByUserId() throws Exception {
        Integer userId = UserUtil.UserContext();
        return userService.queryByUserId(userId);
    }

    @PutMapping("/userId")
    public ResponseUtil updateUser(@RequestBody User user) throws Exception {
        Integer userId = UserUtil.UserContext();
        user.setId(userId);
        return userService.updateUser(user);
    }

    @GetMapping("/exit")
    public ResponseUtil exit() throws Exception {
        return userService.exit();
    }

    @PostMapping("/avatar")
    public ResponseUtil updateAvatar(MultipartFile file) throws Exception {
        Map<String, Object> map = userService.uploadFile(file, "wanshiwu");
        return ResponseUtil.get(ResponseEnum.OK, map);
    }

    //实名认证接口
    @PostMapping("/cardVerify")
    public ResponseUtil cardVerify(@RequestBody JSONObject jsonObject) throws Exception {
        // 前端传递的参数
        String name = jsonObject.getString("name");    // 前端输入的姓名
        String card = jsonObject.getString("card");    // 前端输入的身份证号
        String frontImg = jsonObject.getString("frontImg"); // 正面纯Base64
        String backImg = jsonObject.getString("backImg");   // 反面纯Base64

        // 调用OCR识别
        CardVerify cardVerify = CardVerifyUtil.cardVerify(frontImg, backImg);

        // 校验识别结果
        if (!cardVerify.getSuccess()) {
            return ResponseUtil.getError(400, cardVerify.getConfig_str());
        }
        // 校验前端输入与OCR识别结果是否一致
        if (!name.equals(cardVerify.getName())) {
            return ResponseUtil.getError(400, "输入的姓名与身份证不一致");
        }
        if (!card.equals(cardVerify.getNum())) { // 注意：身份证号字段是getNum()
            return ResponseUtil.getError(400, "输入的身份证号与身份证不一致");
        }
        Integer userId = UserUtil.UserContext();
        User user = new User();
        user.setId(userId);
        userMapper.updateStatus(user);
        return ResponseUtil.get(ResponseEnum.OK, "认证成功");
    }

    @GetMapping("/avatar/{id}")
    public ResponseUtil findUserAvatar(@PathVariable Integer id) {
        return userService.findUserAvatar(id);
    }

    @PostMapping("/smsLogin")
    public ResponseUtil smsLogin(@RequestParam String mobile, @RequestParam String code) throws JOSEException {
        return userService.smsLogin(mobile, code);
    }
}
